package cn.fanyamin.web.struts;

import java.util.Enumeration;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 *
 * @author Walter Fan
 * @version 1.0
  */
public class LogoutAction extends BaseAction {
    private static Log logger = LogFactory.getLog(LogoutAction.class);

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		
		
		/* clear the cookies */
		Cookie cookies[] = request.getCookies();
		Cookie cookie = null;
		if (cookies != null) {
			for(int i=0;i<cookies.length;i++) {
				cookie = cookies[i];
				//logger.debug(cookie.getName() + cookie.getValue());
				cookie.setMaxAge(0);
				response.addCookie(cookie);
			}
		}
		
		/* clear session */
		Enumeration names = session.getAttributeNames();
		while (names.hasMoreElements()) {
		    String name = (String) names.nextElement();
		    logger.debug("session object: " + name + " = " + session.getAttribute(name));
		    session.removeAttribute(name);
		}
		
		return mapping.findForward("success");
}
}
